<?php

	if(empty($sesionIniciada))
	{
		$sesionIniciada = session_start();
	}

	if(isset($_SESSION["usuario"]))
	{
		$db = new ezSQL_mysql("root","","biblioteca","localhost");
		$usuario = $db->get_row("SELECT *
		 						FROM usuarios
		 						WHERE nick='" . $_SESSION["usuario"] . "'");
?>
<script type="text/javascript">
	$(document).ready(function(){
		$("#mensaje").hide();
		$("#imgCarga").hide();
		$("#beditar").show();
		$("#errorNick").hide();
	    $("#errorPassword").hide();
	    $("#errorCPassword").hide();
	    $("#errorEmail").hide();
	    $("#errorNombre").hide();
	    $("#errorCiudad").hide();
		$("#beditar").click(editarUsuario);
	});

	function validaUsuario()
	  {
	    var valida = true;
	    var patronLetras =/^[a-zA-Z]+$/;
	    var patronNombre = /^[a-zA-Z\ \'\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00f1\u00d1\u00FC\u00DC]*$/;
	    var patronEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/ ;
	    if($("#nick").val().length == 0 || !patronLetras.test($("#nick").val()))
	    {
	      valida = false;
	      $("#errorNick").html("Solo se admiten letras");
	      $("#errorNick").show();
	    }else{
	      $("#errorNick").hide();
	    }
	    if($("#password").val().length < 6 && $("#password").val().length != 0)
	    {
	      valida = false;
	      $("#errorPassword").html("El password debe ser de 6 caracteres como minimo");
	      $("#errorPassword").show();
	    }else{
	      $("#errorPassword").hide();
	    }
	    if($("#password").val() != $("#cpassword").val())
	    {
	      valida = false;
	      $("#errorCPassword").html("No se ha introducido el mismo password");
	      $("#errorCPassword").show();
	    }else{
	      $("#errorCPassword").hide();
	    }
	    if($("#email").val().length == 0 || !patronEmail.test($("#email").val()))
	    {
	      valida = false;
	      $("#errorEmail").html("Introduzca un email correcto");
	      $("#errorEmail").show();
	    }else{
	      $("#errorEmail").hide();
	    }

	    if($("#nombre").val().length == 0 ||!patronNombre.test($("#nombre").val()))
	    {
	      valida = false;
	      $("#errorNombre").html("Introduzca un nombre correcto");
	      $("#errorNombre").show();
	    }else{
	      $("#errorNombre").hide();
	    }

	    if($("#ciudad").val().length == 0 || !patronNombre.test($("#ciudad").val()))
	    {
	      valida = false;
	      $("#errorCiudad").html("Introduzca una ciudad correcta");
	      $("#errorCiudad").show();
	    }else{
	      $("#errorCiudad").hide();
	    }

	    return valida;
	  }
	function editarUsuario()
	{
		if(validaUsuario())
    	{
			$.ajax({
	  			type: "POST",
	  			url: "modulos/usuarios/edusuaction.php",
	  			data: {
	  				nick: $("#nick").val(),
	  				password: $("#password").val(),
	  				email: $("#email").val(),
	  				nombre: $("#nombre").val(),
	  				sexo: $("#sexo").val(),
	  				nacimiento: $("#nacimiento").val(),
	  				ciudad: $("#ciudad").val(),
	  				pais: $("#paisper").val()
	  			},
	  			beforeSend: function(){
	  				$("#mensaje").hide();
	  				$("#imgCarga").show();
	  				$("#beditar").hide();
	  			},
	  			success: function(resp){
	  				$("#imgCarga").hide();
	  				$("#beditar").hide();
	  				if (resp == "ok")
	  				{
	  					$("#mensaje").html("Sus datos se han editado correctamente");
	  				}else{
	  					$("#mensaje").html(resp);
	  				}
	  				$("#beditar").show();
	  				$("#mensaje").show();
	  			}
			});
		}
	}
</script>
<?php include("menupanel.php"); ?>
<div id="contenidoPanel">
	<div id="titulosPer">
		<div id="fontit">
			<h3>Mi Perfil</h3>
		</div>
	</div>
<form id="perfil">
	<div><label for="nick">Nick: </label><input id="nick" name="nick" type="text" maxlength="15" value="<?php echo htmlentities($usuario->nick); ?>" readonly/></div>
	<div id="errorNick"></div>
	<div><label for="password">Password: </label><input id="password" name="password" type="password" maxlength="15"/></div>
	<div id="errorPassword"></div>
	<div><label for="cpassword">Repetir Password: </label><input id="cpassword" name="cpassword" type="password" maxlength="15"></div>
   <div id="errorCPassword"></div>
	<div><label for="email">Email: </label><input id="email" name="email" type="text" maxlength="50" value="<?php echo htmlentities($usuario->email); ?>"/></div>
	<div id="errorEmail"></div>
	<div><label for="nombre">Nombre: </label><input id="nombre" name="nombre" type="text" maxlength="50" value="<?php echo htmlentities($usuario->nombre); ?>"/></div>
	<div id="errorNombre"></div>
	<div>
		<label for="nick">Sexo: </label>
		<select id="sexo" name="sexo">
			<option value="hombre" <?php if($usuario->sexo == "hombre")
										{
											echo "selected";
										}?>>Hombre</option>
			<option value="mujer" <?php if($usuario->sexo == "mujer")
										{
											echo "selected";
										}?>>Mujer</option>
		</select>
	</div>
	<div><label for="nacimiento">A&ntilde;o nacimiento: </label>
      <select name="nacimiento" id="nacimiento">
      <?php
      for($anyo=(date("Y")); 1920<=$anyo; $anyo--) {
      	if($anyo == htmlentities($usuario->nacimiento))
      	{
      		echo "<option value='".$anyo."' selected>".$anyo."</option>";
      	}else{
      		echo "<option value='".$anyo."'>".$anyo."</option>";
      	}
      }
      ?>
    </select>
  </div>
	<div><label for="ciudad">Ciudad: </label><input id="ciudad" name="ciudad" type="text" maxlength="50" value="<?php echo htmlentities($usuario->ciudad); ?>"/></div>
	<div id="errorCiudad"></div>
	<div><label for="pais">Pais: </label>
    <select name="pais" id="paisper" class="count">
    <?php
      $array_paises = array("Estados Unidos","Espa&ntilde;a","Republica Dominicana","Afganistan","Africa del Sur","Albania","Alemania","Andorra","Angola","Antigua y Barbuda","Antillas Holandesas","Arabia Saudita","Argelia","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarusia","Belgica","Belice","Benin","Bermudas","Bolivia","Bosnia","Botswana","Brasil","Brunei Darussulam","Bulgaria","Burkina Faso","Burundi","Butan","Camboya","Camerun","Canada","Cape Verde","Chad","Chile","China","Chipre","Colombia","Comoros","Congo","Corea del Norte","Corea del Sur","Costa de Marf&iacute;l","Costa Rica","Croasia","Cuba","Dinamarca","Djibouti","Dominica","Ecuador","Egipto","El Salvador","Emiratos Arabes Unidos","Eritrea","Eslovenia","Estonia","Etiopia","Fiji","Filipinas","Finlandia","Francia","Gabon","Gambia","Georgia","Ghana","Granada","Grecia","Groenlandia","Guadalupe","Guam","Guatemala","Guayana Francesa","Guerney","Guinea","Guinea-Bissau","Guinea Equatorial","Guyana","Haiti","Holanda","Honduras","Hong Kong","Hungria","India","Indonesia","Irak","Iran","Irlanda","Islandia","Islas Caiman","Islas Faroe","Islas Malvinas","Islas Marshall","Islas Solomon","Islas Virgenes Britanicas","Islas Virgenes (U.S.)","Israel","Italia","Jamaica","Japon","Jersey","Jordania","Kazakhstan","Kenia","Kiribati","Kuwait","Kyrgyzstan","Laos","Latvia","Lesotho","Libano","Liberia","Libia","Liechtenstein","Lituania","Luxemburgo","Macao","Macedonia","Madagascar","Malasia","Malawi","Maldivas","Mali","Malta","Marruecos","Martinica","Mauricio","Mauritania","Mexico","Micronesia","Moldova","Monaco","Mongolia","Mozambique","Myanmar (Burma)","Namibia","Nepal","Nicaragua","Niger","Nigeria","Noruega","Nueva Caledonia","Nueva Zealandia","Oman","Pakistan","Palestina","Panama","Papua Nueva Guinea","Paraguay","Peru","Polinesia Francesa","Polonia","Portugal","Puerto Rico","Qatar","Reino Unido","Republica Centroafricana","Republica Checa","Republica Democratica del Congo","Republica Eslovaca","Reunion","Ruanda","Rumania","Rusia","Sahara","Samoa","San Cristobal-Nevis (St. Kitts)","San Marino","San Vincente y las Granadinas","Santa Helena","Santa Lucia","Santa Sede (Vaticano)","Sao Tome & Principe","Senegal","Seychelles","Sierra Leona","Singapur","Siria","Somalia","Sri Lanka (Ceilan)","Sudan","Suecia","Suiza","Sur Africa","Surinam","Swaziland","Tailandia","Taiwan","Tajikistan","Tanzania","Timor Oriental","Togo","Tokelau","Tonga","Trinidad & Tobago","Tunisia","Turkmenistan","Turquia","Ucrania","Uganda","Union Europea","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam","Yemen","Yugoslavia","Zambia","Zimbabwe");
      $cantidad_paises = count($array_paises);
      for($i = 0; $i<$cantidad_paises; $i++){
          $array_paises_i = $array_paises[$i];
          if($array_paises_i == htmlentities($usuario->pais))
          {
          	echo "<option value='".$array_paises_i."' selected>".$array_paises_i."</option>";
          }else{
          	echo "<option value='".$array_paises_i."'>".$array_paises_i."</option>";
          }
      }
      ?>
    </select>
    <div class="limpia"></div>
	<input id="beditar" type="button" value="Editar" />
	<img id="imgCarga" src="estilos/img/carga.gif"/>
	<div id="mensaje"></div>
</form>
</div>
<div class="limpia"></div>

<?php
	}else{
		header("location: index.php?section=usuarios");
	}
?>